# coding: utf-8
"""
思兮数据导入
"""
from __future__ import unicode_literals
import datetime
import os
import sys
import django


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from apps.order.models import BaseOrder
from apps.sales_man.models import VisitApplicant
from apps.sales_man.models import Single
from apps.utils.views import YouCardApi

"""
首先通过SalesSign找到推荐人
然后将推荐人和数据放到BaseOrder中
"""


class InsertOrder(YouCardApi):
    def insert_data(self):
        data_list = self.visit_list()
        data_list = data_list.get("data")
        # data_list = [{u'username': u'\u9ec4\u626c', u'bank_name': u'\u6d66\u53d1\u94f6\u884c', u'visit_id': 72371,
        #               u'is_second': 0, u'visit_date': u'2018-09-12', u'status': u'\u6210\u529f',
        #               u'product_name': u'\u6d66\u53d1\u94f6\u884c\u4fe1\u7528\u5361'},
        #              {u'username': u'\u620e\u5a07', u'bank_name': u'\u6c11\u751f\u94f6\u884c', u'visit_id': 71550,
        #               u'is_second': 0, u'visit_date': u'2018-09-12', u'status': u'\u6210\u529f',
        #               u'product_name': u'\u6c11\u751f\u4fe1\u7528\u5361(\u5168\u5361\u79cd)'},
        #              {u'username': u'\u9a6c\u8d75\u51b2', u'bank_name': u'\u6d66\u53d1\u94f6\u884c', u'visit_id': 69308,
        #               u'is_second': 0, u'visit_date': u'2018-09-10', u'status': u'\u6210\u529f',
        #               u'product_name': u'\u6d66\u53d1\u94f6\u884c\u4fe1\u7528\u5361'}]
        not_exist_list = []
        for i in data_list:
            if i.get("status") == "成功":
                visit_id = i.get("visit_id")
                visit_date = i.get("visit_date")
                visit_date = datetime.datetime.strptime(visit_date, str("%Y-%m-%d"))
                visit_apply_queryset = VisitApplicant.objects.filter(visit_id=visit_id)
                if not visit_apply_queryset.exists():
                    not_exist_list.append(visit_id)
                visit_apply_queryset = visit_apply_queryset.exclude(status=4)
                if not visit_apply_queryset.exists():
                    continue
                else:
                    visit_apply_obj = visit_apply_queryset.first()
                apply = visit_apply_obj.applicant
                single_queryset = Single.objects.filter(applicant=apply,
                                                        product=visit_apply_obj.product,
                                                        create_time__year=visit_date.year,
                                                        create_time__month=visit_date.month,
                                                        create_time__day=visit_date.day
                                                        )
                if single_queryset.count() == 1:
                    single_obj = single_queryset.first()
                else:
                    result_list = single_queryset.values('b_user', "t_user").distinct()
                    if result_list.count() == 1:
                        single_obj = single_queryset.first()
                    else:
                        raise Exception("数据错误")
                BaseOrder.objects.insert_order(single_obj, visit_date, cash=None)
                visit_apply_obj.status = 4
                visit_apply_obj.save()
        print(not_exist_list)


if __name__ == '__main__':
    insert_order = InsertOrder()
    insert_order.insert_data()
    pass
